/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mailctrl;

/**
 *
 * @author flintx
 */
//import java.io.FileOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class MailCreater {
    boolean created;
    Element root;
    static int operID;
    Document doc;
    public MailCreater()
    {
        created = false;
        operID = 0;
    }
    public String buildXMLDoc(){
        root = new Element("list");
        doc = new Document(root);
        Element elements;
        for(int i=0; i<5; i++)
        {
            elements = new Element("user");
            elements.setAttribute("id", ""+i);
            elements.addContent(new Element("name").setText("flintx"));
            elements.addContent(new Element("age").setText("27"));
            elements.addContent(new Element("sex").setText("Male"));
            root.addContent(elements);
        }
        Format format = Format.getCompactFormat();

        format.setEncoding("gb2312");           //设置xml文件的字符为gb2312

        format.setIndent("    ");               //设置xml文件的缩进为4个空格

        XMLOutputter XMLOut = new XMLOutputter(format);//在元素后换行，每一层元素缩排四格
//        XMLOut.output(doc, new FileOutputStream("user.xml"));
        System.out.print(XMLOut.outputString(doc));
        return XMLOut.outputString(doc);
    }
    public void newXMLMail()
    {
        root = new Element("list");
        doc = new Document(root);
        Element apps = new Element("Applications");
        root.addContent(apps);
    }
    public Element packageOperation(String appName, String appPath, List<String> parameters)
    {
        if (!appName.equals(null) && appName != null && !appName.equals("")) {
            Element element = new Element("Operation");
            element.addContent(new Element("Name").setText(appName));
            element.addContent(new Element("Path").setText(appPath));
            if (parameters != null) {
                Element params = new Element("Parameters");
                params.setAttribute("total", "" + parameters.size());
                for (int i = 0; i < parameters.size(); i++) {
                    String param = parameters.get(i);
                    params.addContent(new Element("Parameter").setText(param));
                }
            }
            return element;
        } else {
            return null;
        }
        //root.addContent(element);
    }
    public void addOperation(Element opElement)
    {
        if(!opElement.equals(null))
        {
            opElement.setAttribute("id", "" + operID);
            operID++;
            Element apps = root.getChild("Applications");
            apps.addContent(opElement);
        }
    }
    public void delOperation(int opID)
    {
        Element apps = root.getChild("Applications");
        List operList = apps.getChildren("Operation");
        for(int i=0; i<operList.size(); i++)
        {
            Element app = (Element) operList.get(i);
            int id = Integer.parseInt(app.getAttributeValue("id"));
            if(opID == id)
            {
                apps.removeContent(app);
                break;
            }
        }
    }
    public void modifyOperation(int opID, Element newElement) {
        
        if (!newElement.equals(null) && newElement != null) {
            Element apps = root.getChild("Applications");
            List operList = apps.getChildren("Operation");
            for (int i = 0; i < operList.size(); i++) {
                Element app = (Element) operList.get(i);
                int id = Integer.parseInt(app.getAttributeValue("id"));
                if (opID == id) {
                    //app.setAttribute("id", newElement.getAttributeValue("id"));
                    (app.getChild("Name")).setText(newElement.getChildText("Name"));
                    (app.getChild("Path")).setText(newElement.getChildText("Path"));
                    Element params = newElement.getChild("Parameters");
                    if (params != null && params.equals(null)) {
                        app.removeChild("Parameters");
                        Element newparams = new Element("Parameters");
                        List paramList = params.getChildren("Parameter");
                        for (int j = 0; j < paramList.size(); j++) {
                            Element param = (Element) paramList.get(j);
                            String value = param.getText();
                            newparams.addContent(new Element("Parameter").setText(value));
                        }
                    }

                }
                break;
            }
        }
    }

    public String buildMailContent() throws IOException {
        Format format = Format.getCompactFormat();
        format.setEncoding("gb2312");           //设置xml文件的字符为gb2312
        format.setIndent("    ");               //设置xml文件的缩进为4个空格
        XMLOutputter XMLOut = new XMLOutputter(format);
        XMLOut.output(doc, new FileOutputStream("user.xml"));               
        System.out.print(XMLOut.outputString(doc));
        return XMLOut.outputString(doc);
    }
    public boolean getFlag()
    {
        return this.created;
    }
}
